﻿using System;
using System.Collections.Generic;
using System.Linq;
using FantasyBlog.DAL;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            BlogModelContainer container = new BlogModelContainer();
            var role = new Role();
            var user = new User();
            role.name = "testrole";
            role.description = "testdes";
            //container.RoleSet.Add(role);
            //container.SaveChanges();
            //container.SaveChanges();
            user.Roles.Add(container.RoleSet.Find("testrole"));
            user.username = "testuser";
            user.registered = DateTime.Now;
            user.displayname = "Baichixxh";
            user.email = "test@gest.com";
            user.password = "123123";
            user.status = "etst";
            container.UserSet.Add(user);
            try
            {
                container.SaveChanges();
            }
            catch (System.Data.Entity.Validation.DbEntityValidationException ex)
            {
                foreach (var e in ex.EntityValidationErrors)
                    foreach (var ee in e.ValidationErrors)
                        Console.WriteLine(ee.ErrorMessage);
            }
            var users = (from u in container.UserSet
                         where u.username == "testuser" && u.Roles.Contains((
                             from r in container.RoleSet
                             where r.name == "testrole"
                             select r).SingleOrDefault())
                         select u.Roles).SingleOrDefault();
            foreach (var u in users)
            {
                Console.WriteLine(u);
            }
        }
    }
}
